//
//  KDChooseDateView.m
//  LaiKeDaoClient
//
//  Created by dongkedi的iMac on 15/11/6.
//  Copyright © 2015年 LKD. All rights reserved.
//

#import "KDChooseDateView.h"

@implementation KDChooseDateView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setFrame:(CGRect){0,0,kScreenWidth,kScreenHeight}];
        
        UIView  *backView = [[UIView alloc] initWithFrame:self.bounds];
        [backView setBackgroundColor:[UIColor blackColor]];
        [backView setAlpha:0.2];
        [self addSubview:backView];
        
        [self createUI];
        [self createButtons];
    }
    return self;
}

- (void)createUI
{
    self.dateView = [[UIView alloc] initWithFrame:(CGRect){0,kScreenHeight-284-35*3-20*2-10,kScreenWidth,284+35*3+20*2+10}];
    [_dateView setBackgroundColor:[UIColor whiteColor]];
    [self addSubview:_dateView];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:(CGRect){0,0,WIDTH(_dateView),35}];
    [_titleLabel setTextAlignment:NSTextAlignmentCenter];
    [_titleLabel setFont:[UIFont systemFontOfSize:12]];
    [_titleLabel setBackgroundColor:[UIColor clearColor]];
    [_titleLabel setTextColor:[UIColor blackColor]];
    [_titleLabel setText:@"请选择日期"];
    [_dateView addSubview:_titleLabel];
    
    UIView  *lineView = [[UIView alloc] initWithFrame:(CGRect){0,36,WIDTH(_dateView),1}];
    [lineView setBackgroundColor:[UIColor colorWithHexString:@"#cccccc"]];
    [_dateView addSubview:lineView];
    
    UIView  *calendarView = [[UIView alloc] initWithFrame:(CGRect){0,35,kScreenWidth,284}];
    [calendarView setUserInteractionEnabled:YES];
    [calendarView setBackgroundColor:[UIColor clearColor]];
    [_dateView addSubview:calendarView];
    
    self.pickerView = [KDCalendarPicker showOnView:calendarView];
    self.pickerView.type = 1;
    self.pickerView.today = [NSDate date];
    self.pickerView.date = self.pickerView.today;
    self.pickerView.pickerDelegate = self;
}

- (void)pickerClickDay:(NSInteger)day Month:(NSInteger)month Year:(NSInteger)year
{
    self.dateStr = [NSString stringWithFormat:@"%ld-%ld-%ld",(long)year,(long)month,(long)day];
}

- (void)createButtons
{
    float   ButtonWidth = kScreenWidth-64;
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button1 setFrame:(CGRect){32,36+284+20,ButtonWidth,70/2}];
    [button1 addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
    [button1 setBackgroundColor:kNavBlueColor];
    [button1.titleLabel setTextColor:[UIColor whiteColor]];
    [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button1 setTitle:@"确  定" forState:UIControlStateNormal];
    [_dateView addSubview:button1];
    button1.tag = 101;
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button2 setFrame:(CGRect){32,Y(button1)+HEIGHT(button1)+10,ButtonWidth,70/2}];
    [button2 addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
    [button2 setBackgroundColor:[UIColor whiteColor]];
    [button2.titleLabel setTextColor:[UIColor blackColor]];
    button2.layer.cornerRadius = 0;//设置那个圆角的有多圆
    button2.layer.borderWidth = 1;//设置边框的宽度，
    button2.layer.borderColor = [UIColor colorWithHexString:@"#cccccc"].CGColor;
    button2.layer.masksToBounds = YES;
    [button2 setTitle:@"取  消" forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_dateView addSubview:button2];
    button2.tag = 102;
}

- (void)buttonAction1:(UIButton *)button
{
    switch (button.tag) {
        case 101:{
            if (self.dateStr.length > 0) {
                if ([self.sureDelegate respondsToSelector:@selector(clickSure:)]) {
                    [self.sureDelegate clickSure:self.dateStr];
                }
                [self hiden];
            }
            
        }
            break;
        case 102:
            [self hiden];
            break;
        
        default:
            break;
    }
}

- (void)show
{
    [kWindow addSubview:self];
}

- (void)hiden
{
    [self removeFromSuperview];
}

@end
